"""
-*- coding: utf-8 -*-
@Time : 2021/9/17 23:58
@Author : chenke
@File : base.py
"""
import datetime

import appium
from appium import webdriver
from appium.common.exceptions import NoSuchContextException
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from common.logger import logger
from utils.base_path import root_dir


class BasePage:
    # driver:webdriver

    def __init__(self,driver):
        self.driver = driver


    def find_element(self,loc,msg=' '):
        try:
            logger.info("定位元素：{}，元素描述：{}".format(loc,msg))
            # ele = self.driver.find_element(loc)
            ele = WebDriverWait(self.driver,10).until(EC.visibility_of_element_located(loc))
        except Exception as e:
            logger.error("元素定位失败，失败截图{}".format(self.get_screenshot()))
            raise e
        else:
            logger.info("元素定位成功！")
            return ele

    def find_elements(self,loc,msg=" "):
        try:
            logger.info("定位元素：{}，元素描述：{}".format(loc,msg))
            eles = WebDriverWait(self.driver,10).until(EC.visibility_of_element_located(loc))
        except Exception as e:
            logger.error("元素定位失败!失败截图{}".format(self.get_screenshot()))
            raise e
        else:
            logger.info("元素定位成功！")
            return eles

    def swipe_to_left(self):
        '''
        向左滑动屏幕
        :return:
        '''
        window_size = self.driver.get_window_size()
        x = window_size["width"]
        y = window_size["height"]
        logger.info("向左滑动屏幕")
        self.driver.swipe(x*3//4,y//2,x//4,y//2,500)

    def swipe_to_right(self):
        '''
        向右滑动屏幕
        :return:
        '''
        window_size = self.driver.get_window_size()
        x = window_size["width"]
        y = window_size["height"]
        logger.info("向右滑动屏幕")
        self.driver.swipe(x//4,y//2,x*3//4,y//2,500)

    def swipe_to_up(self):
        '''
        向上滑动屏幕
        :return:
        '''
        window_size = self.driver.get_window_size()
        x = window_size["width"]
        y = window_size["height"]
        logger.info("向上滑动屏幕")
        self.driver.swipe(x//2,y*2//3,x//2,y//3,500)

    def swipe_to_down(self):
        '''
        向下滑动屏幕
        :return:
        '''
        window_size = self.driver.get_window_size()
        x = window_size["width"]
        y = window_size["height"]
        logger.info("向下滑动屏幕")
        self.driver.swipe(x//2,y//3,x//2,y*2//3,500)


    def press_key(self,key):
        '''

        :param key: power=4,volume_up=24,volume_down=25,home=3,back=4
        '''
        keys=dict(power=4,volume_up=24,volume_down=25,home=3,back=4)
        if key in keys.keys():
            key_code = keys[key]
            logger.info("点击{}键".format(key))
            self.driver.keyevent(key_code)
        else:
            logger.warning("输入的key不存在")

    def element_click(self,loc):
        ele = self.find_element(loc)
        logger.info("点击元素：{}".format(loc))
        ele.click()

    def input_text(self,loc,text=""):
        ele = self.find_element(loc)
        logger.info("给元素：{}输入文本：{}".format(loc,text))
        ele.send_keys(text)

    def get_screenshot(self):
        now_time = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
        image_path = root_dir + r"\report\screenshots\{}.png".format(now_time)
        logger.info("获取当前手机界面截图，存放路径：{}".format(image_path))
        self.driver.get_screenshot_as_file(image_path)
        return image_path




